import numpy as np
import pandas as pd
from sklearn.svm import SVC

train_data = pd.read_csv('diabetes_train.data')
test_data = pd.read_csv('diabetes_test.data')
train_data['Class'] = train_data['Class variable'].map({'tested_negative': 0, 'tested_positive': 1})
test_data['Class'] = test_data['Class variable'].map({'tested_negative': 0, 'tested_positive': 1})

train_X = np.array(train_data.iloc[:, 0:8])
test_X = np.array(test_data.iloc[:, 0:8])
train_Y = np.array(train_data.iloc[:, 9:])
test_Y = np.array(test_data.iloc[:, 9:])

'''
linear: 线性核函数
poly: 多项式核函数
rbf: 径像核函数/高斯核
sigmoid: sigmoid核函数
precomputed: 核矩阵
'''
model = SVC(kernel='rbf')
model.fit(train_X, train_Y.ravel())

y_pred = model.predict(test_X)

'''
# 手算均方根误差
sum_mean = 0
for i in range(len(y_pred)):
    sum_mean += (y_pred[i] - test_Y[i][0]) ** 2
sum_err = np.sqrt(sum_mean / len(y_pred))  # 测试级的数量

print("RMSE by hand: {:.4f}%".format(sum_err * 100))
'''

# sklearn自带的评估
score = model.score(test_X, test_Y)
print("sklearn score: {:.4f}%".format(score * 100))

# 计算成功率：成功率计算方式：病情预测正确的病人数量 / 所有测试集的数据数量（要求预测成功率在 50% 以上）
success = 0
for i in range(len(y_pred)):
    if y_pred[i] == test_Y[i][0]:
        success += 1
accu = success / len(test_data)
print("预测成功率：{:.4f}%".format(accu * 100))
